import os
import xunjian
import pub_config
import argparse
import pandas as pd
import pub_readconf
import pub_sendMail


# 巡检服务
def use_xunjian():
    xj_file = pub_readconf.ReadConf(os.path.join(pub_config.ROOT_PATH, "conf/xunjian.yml"))
    dict_list = []
    for server in xj_file.read_all_yaml():
        dict_data = xunjian.xj_unix_os(server)
        dict_list.append(dict_data)
    # 将字典列表转成二维表格
    df = pd.DataFrame(data=dict_list)
    file_path = os.path.join(pub_config.ROOT_PATH, "result/xj_unix_os_{}.xlsx".format(pub_config.NOW_TIME))
    df.to_excel(file_path)
    # 发邮件
    msgdata = "请查看附件"
    # 邮件显示文件名
    filename = os.path.basename(file_path)
    mail = pub_sendMail.SendMail(msgdata, file_path, filename)
    mail.send_email()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', type=str, default="conf/xunjian.yml", help="指定配置文件巡检")
    args = parser.parse_args()
    if args.c == 'None':
        print("monitor.py -h 查看帮助命令")
    else:
        use_xunjian()
